package caat.application.dao;

import java.sql.Connection;
import java.util.Collection;
import java.util.Vector;

import caat.framework.datamanager.AppDAO;
import caat.framework.exception.DAOException;
import caat.framework.exception.DatabaseConnectionFailureException;
import caat.framework.exception.SelectStatementException;

@SuppressWarnings("serial")
public class FunctionDAO extends AppDAO {
	private static String CLASS_NAME_DAO = "caat.application.dao.FunctionDAO";
	private static String CLASS_NAME_VO = "caat.application.entityobject.FunctionEO";
	private static String STRING_EMPTY = "";
	Connection conn = null;

	public FunctionDAO(Connection conn) {
		super();
		this.conn = conn;
	}
	
	@SuppressWarnings("rawtypes")
	public Collection getFunctionAdmin(String whereClause,Vector params) throws IllegalArgumentException, DatabaseConnectionFailureException, SelectStatementException, DAOException{
		StringBuffer sbSQL = new StringBuffer();
		try {
			sbSQL.append("SELECT a.id,a.functionId,a.functionName,a.functionDes,a.functionLevel,a.parentId,a.functionType,a.functionMethod FROM tblFunction a ");
			if(whereClause!=null && !STRING_EMPTY.equals(whereClause)){
				sbSQL.append("where ");
				sbSQL.append(whereClause);
			}
			sbSQL.append("order by a.parentId");
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
			throw new DAOException(CLASS_NAME_DAO + ".getFunctionAdmin(): " +
                    ex.getMessage(), ex);
		}
		return executeSelectStatement(sbSQL.toString(), params, CLASS_NAME_VO, conn);
	}
	@SuppressWarnings("rawtypes")
	public Collection getFunctionAdminByKey(String whereClause,Vector params) throws IllegalArgumentException, DatabaseConnectionFailureException, SelectStatementException, DAOException{
		StringBuffer sbSQL = new StringBuffer();
		try {
			sbSQL.append("SELECT a.id,a.functionId,a.functionName,a.functionDes,a.functionLevel,a.parentId,a.functionType,a.functionMethod FROM tblFunction a ");
			if(whereClause!=null && !STRING_EMPTY.equals(whereClause)){
				sbSQL.append("where ");
				sbSQL.append(whereClause);
			}
			sbSQL.append("order by a.parentId");
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
			throw new DAOException(CLASS_NAME_DAO + ".getFunctionAdminByKey(): " +
                    ex.getMessage(), ex);
		}
		return executeSelectStatement(sbSQL.toString(), params, CLASS_NAME_VO, conn);
	}
}
